 // 百度地图API功能
 var map = new BMap.Map("allmap",{mapType:BMAP_SATELLITE_MAP});
 var point = new BMap.Point(116.404, 39.915);
 map.centerAndZoom(point, 12);
 map.enableScrollWheelZoom();   //启用滚轮放大缩小，默认禁用
 map.enableContinuousZoom();    //启用地图惯性拖拽，默认禁用

 console.log(sensorsOfNormalDatas)
 var sensorsOfNormals = []
//  var sensorsOfNormal=[
//   {"equ_id":0, "equ_name":'刘林', "equ_state":0, "equ_power":89.3, "equ_scope":500, "equ_lng":116.404, "equ_lat":39.989, "equ_voltage":'200v'},
//   {"equ_id":1, "equ_name":'刘林1', "equ_state":0, "equ_power":89.3, "equ_scope":500, "equ_lng":116.414, "equ_lat":39.939, "equ_voltage":'200v'},
//   {"equ_id":2, "equ_name":'刘林2', "equ_state":0, "equ_power":89.3, "equ_scope":500, "equ_lng":116.424, "equ_lat":39.949, "equ_voltage":'200v'},
//   {"equ_id":3, "equ_name":'刘林', "equ_state":0, "equ_power":89.3, "equ_scope":500, "equ_lng":116.434, "equ_lat":39.919, "equ_voltage":'200v'},
//   {"equ_id":4, "equ_name":'刘林', "equ_state":0, "equ_power":89.3, "equ_scope":500, "equ_lng":116.444, "equ_lat":39.999, "equ_voltage":'200v'},
//   {"equ_id":5, "equ_name":'刘林', "equ_state":0, "equ_power":89.3, "equ_scope":500, "equ_lng":116.454, "equ_lat":39.959, "equ_voltage":'200v'},
//   {"equ_id":6, "equ_name":'刘林', "equ_state":0, "equ_power":89.3, "equ_scope":500, "equ_lng":116.464, "equ_lat":39.979, "equ_voltage":'200v'},
//   {"equ_id":7, "equ_name":'刘林', "equ_state":0, "equ_power":89.3, "equ_scope":500, "equ_lng":116.494, "equ_lat":39.929, "equ_voltage":'200v'},
//  ]

function getdata() {
  console.log("a")
  for(let i=0; i<sensorsOfNormalDatas.length; i++){
    let sensorsOfNormal = {
      "equ_id":sensorsOfNormalDatas[i].equ_id, 
      "equ_name":sensorsOfNormalDatas[i].equ_name, 
      "equ_state":sensorsOfNormalDatas[i].equ_state, 
      "equ_power":sensorsOfNormalDatas[i].equ_power, 
      "equ_scope":sensorsOfNormalDatas[i].equ_scope, 
      "equ_lng":sensorsOfNormalDatas[i].equ_lng, 
      "equ_lat":sensorsOfNormalDatas[i].equ_lat, 
      "equ_voltage":sensorsOfNormalDatas[i].equ_voltage
    }
    sensorsOfNormals.push(sensorsOfNormal)
  }
}
getdata()


// var sensorsOfNormal=[
//   {"equ_id":sensorsOfNormalDatas.equ_id, "equ_name":sensorsOfNormalDatas.equ_name, "equ_state":sensorsOfNormalDatas.equ_state, "equ_power":sensorsOfNormalDatas.equ_power, "equ_scope":sensorsOfNormalDatas.equ_scope, "equ_lng":sensorsOfNormalDatas.equ_lng, "equ_lat":sensorsOfNormalDatas.equ_lat, "equ_voltage":sensorsOfNormalDatas.equ_voltage},
//   {"equ_id":1, "equ_name":'刘林1', "equ_state":0, "equ_power":89.3, "equ_scope":500, "equ_lng":116.414, "equ_lat":39.939, "equ_voltage":'200v'},
//   {"equ_id":2, "equ_name":'刘林2', "equ_state":0, "equ_power":89.3, "equ_scope":500, "equ_lng":116.424, "equ_lat":39.949, "equ_voltage":'200v'},
//   {"equ_id":3, "equ_name":'刘林', "equ_state":0, "equ_power":89.3, "equ_scope":500, "equ_lng":116.434, "equ_lat":39.919, "equ_voltage":'200v'},
//   {"equ_id":4, "equ_name":'刘林', "equ_state":0, "equ_power":89.3, "equ_scope":500, "equ_lng":116.444, "equ_lat":39.999, "equ_voltage":'200v'},
//   {"equ_id":5, "equ_name":'刘林', "equ_state":0, "equ_power":89.3, "equ_scope":500, "equ_lng":116.454, "equ_lat":39.959, "equ_voltage":'200v'},
//   {"equ_id":6, "equ_name":'刘林', "equ_state":0, "equ_power":89.3, "equ_scope":500, "equ_lng":116.464, "equ_lat":39.979, "equ_voltage":'200v'},
//   {"equ_id":7, "equ_name":'刘林', "equ_state":0, "equ_power":89.3, "equ_scope":500, "equ_lng":116.494, "equ_lat":39.929, "equ_voltage":'200v'},
//  ]

 var sensorsOfAlter = [
   {"equ_mainid": 0, "equ_cid": 2, "siren_time":"2020-10-02 15:38:40", "siren_type":0, "siren_rank": "地震", "equ_lng":116.424, "equ_lat":39.949},
   {"equ_mainid": 1, "equ_cid": 4, "siren_time":"2020-10-02 15:38:40", "siren_type":0, "siren_rank": "地震", "equ_lng":116.444, "equ_lat":39.999}
 ]

 var sensorsOfFault = [
   {"equ_cid": 1, "fault_type": "断电", "equ_lng":116.414, "equ_lat":39.939}
 ]


 var positions = [[116.404, 39.915],[116.414, 39.913]] //坐标记集合点
 var positions = [] //坐标记集合点
 var markers = []   // marker的集合
 var alertPoint = [] //警报的标记
 var faultPoint = [] //警报的标记


 var myIcon1 = new BMap.Icon('../image/00.png',new BMap.Size(50,39));//自定义图标

 for(let i=0; i<sensorsOfNormals.length; i++){
   var point = new BMap.Point(this.sensorsOfNormals[i].equ_lng, this.sensorsOfNormals[i].equ_lat)
   addMarker(point, i);
 }
 
 
 // 编写自定义函数,创建标注
 function addMarker(point, i){
   var marker = new BMap.Marker(point,{icon:myIcon1});
   map.addOverlay(marker);
  //  标记的点击
   marker.addEventListener("click", function(){   
     //把i传过去展示出信息就可以从sensors 拿数据    
    infoWindowOfNormal(point, i)
   });
   markers.push(marker)
 }


//大警告
 // 西南角和东北角
 var SW = new BMap.Point(116.29579,39.837146);
 var NE = new BMap.Point(116.475451,39.9764);

 groundOverlayOptions = {
   opacity: 20,
   displayOnMinLevel: 7,
   displayOnMaxLevel: 16
 }
 //警告图标出现与小图标的联系
 var makerLinkWanning = null;
 FMakerLinkWanning(makerLinkWanning)
 setTimeout(function(){
   makerLinkWanning = 1
   FMakerLinkWanning(makerLinkWanning)
   //模拟警报
   for(let i=0; i<sensorsOfAlter.length; i++){
    replaceOfAlterMarker(i)
    // replaceMarker(2, sensorsOfAlter[i].equ_cid)
   }
  //  模拟故障
  for(let i=0; i<sensorsOfFault.length; i++){
    replaceOfFaultMarker(i)
    // replaceMarker(2, sensorsOfAlter[i].equ_cid)
   }
   console.log(makerLinkWanning)
 }, 3000 )

 function FMakerLinkWanning(i){
   if( i != null){
     addGroundOverlay()
   }
 }
 

 // 初始化GroundOverlay
 var groundOverlay = new BMap.GroundOverlay(new BMap.Bounds(SW, NE), groundOverlayOptions);

 // 设置GroundOverlay的图片地址
 groundOverlay.setImageURL('../image/wanning.gif');


 // 单击事件
 groundOverlay.addEventListener('click', function (clickEvent) {
   removeGroundOverlay()
   // map.centerAndZoom(new BMap.Point(positions[makerLinkWanning], positions[makerLinkWanning]), 18);
   map.centerAndZoom(positions[0], 18);
   infoWindowOfAlert(0)
   console.log('clickEvent', clickEvent);
 });

 // 双击事件
 groundOverlay.addEventListener('dblclick', function (dblclickEvent) {
 // console.log('dblclickEvent', dblclickEvent);
 });

 function addGroundOverlay() {
 // 添加GroundOverlay
 map.addOverlay(groundOverlay);
 }

 function removeGroundOverlay() {
 // 移除GroundOverlay
 map.removeOverlay(groundOverlay);
 }
 
//  //删除原有的图标，替换警报图标
//  function replaceMarker (alarmAndFaultSignal, cid, alterId){
//   //  警报或者故障修护成正常
//    if(alarmAndFaultSignal == 0){

//    }else if(alarmAndFaultSignal == 1){
//     //警报换图标
//      var allOverlay = map.getOverlays();
//      //循环删除原有的图标
//      for(var j = 0;j<allOverlay.length;j++) {
//          //删除指定经度的点
//          if (allOverlay[j].toString()=="[object Marker]" && allOverlay[j].getPosition().lng ==sensorsOfNormal[cid].equ_lng && allOverlay[j].getPosition().lat==sensorsOfNormal[cid].equ_lat) {
//              map.removeOverlay(allOverlay[j]);
//          }
//      }
//      //添加新图标
//      var myIcon2 = new BMap.Icon('../image/02.gif',new BMap.Size(50,39));//自定义图标
//      var point1 = new BMap.Point(sensorsOfNormal[cid].equ_lng, sensorsOfNormal[cid].equ_lat);
//      alertPoint.push(point1)
//      var marker = new BMap.Marker(point1,{icon:myIcon2});
//      map.addOverlay(marker);
//     //  警报marker的点击信息
//      marker.addEventListener("click", function(){      
//       infoWindowOfAlert(alterId)
//     });
//      var circle = new BMap.Circle(point1, 500, {strokeColor:"red", strokeWeight:2, strokeOpacity:0.5})
//      map.addOverlay(circle)
//    }else if(alarmAndFaultSignal == 2) {
//      //故障换图标
//      var allOverlay = map.getOverlays();
//      for(var j = 0;j<allOverlay.length;j++) {
//          //删除指定经度的点
//          if (allOverlay[j].toString()=="[object Marker]" && allOverlay[j].getPosition().lng ==sensorsOfNormal[cid].equ_lng && allOverlay[j].getPosition().lat==sensorsOfNormal[cid].equ_lat) {
//              map.removeOverlay(allOverlay[j]);
//          }
//      }
//      //替换上故障信息
//      var myIcon3 = new BMap.Icon('../image/01.png',new BMap.Size(50,39));//自定义图标
//      var point2 = new BMap.Point(sensorsOfNormal[cid].equ_lng, sensorsOfNormal[cid].equ_lat);
//      var marker2 = new BMap.Marker(point2,{icon:myIcon3});
//      map.addOverlay(marker2);
//    }
//  }


 //删除原有的图标，替换警报图标
 function replaceOfAlterMarker (alterId){
    //警报换图标
     var allOverlay = map.getOverlays();
     //循环删除原有的图标
     for(var j = 0;j<allOverlay.length;j++) {
         //删除指定经度的点
         if (allOverlay[j].toString()=="[object Marker]" && allOverlay[j].getPosition().lng ==sensorsOfAlter[alterId].equ_lng && allOverlay[j].getPosition().lat==sensorsOfAlter[alterId].equ_lat) {
             map.removeOverlay(allOverlay[j]);
         }
     }
     //添加新图标
     var myIcon2 = new BMap.Icon('../image/02.gif',new BMap.Size(50,39));//自定义图标
     var point1 = new BMap.Point(sensorsOfAlter[alterId].equ_lng, sensorsOfAlter[alterId].equ_lat);
     alertPoint.push(point1)
     var marker = new BMap.Marker(point1,{icon:myIcon2});
     map.addOverlay(marker);
    //  警报marker的点击信息
     marker.addEventListener("click", function(){      
      infoWindowOfAlert(alterId)
    });
     var circle = new BMap.Circle(point1, 500, {strokeColor:"red", strokeWeight:2, strokeOpacity:0.5})
     map.addOverlay(circle)
 }


 //删除原有的图标，替换故障图标
 function replaceOfFaultMarker (faultId){
     //故障换图标
     var allOverlay = map.getOverlays();
     for(var j = 0;j<allOverlay.length;j++) {
         //删除指定经度的点
         if (allOverlay[j].toString()=="[object Marker]" && allOverlay[j].getPosition().lng ==sensorsOfFault[faultId].equ_lng && allOverlay[j].getPosition().lat==sensorsOfFault[faultId].equ_lat) {
             map.removeOverlay(allOverlay[j]);
         }
     }
     //替换上故障信息
     var myIcon3 = new BMap.Icon('../image/01.png',new BMap.Size(50,39));//自定义图标
     var point2 = new BMap.Point(sensorsOfFault[faultId].equ_lng, sensorsOfFault[faultId].equ_lat);
     faultPoint.push(point2)
     var marker2 = new BMap.Marker(point2,{icon:myIcon3});
     map.addOverlay(marker2);
     marker2.addEventListener("click", function(){      
      infoWindowOfFault(faultId)
    });
 }

//  正常时信息窗口i
function infoWindowOfNormal(point, i){
  var sContent =`<div class="father">
  <h4>传感器状况</h4>
  <p class="typeOfDisaster">传感器的编号：${sensorsOfNormals[i].equ_id}</p>
  <p class="disasterLevel">传感器状态：${sensorsOfNormals[i].equ_state}</p>
  <p class="longitudeAndLatitude">经纬度：${sensorsOfNormals[i].equ_lng} , ${sensorsOfNormals[i].equ_lat}</p>
  <p class="longitudeAndLatitude">耗电量：${sensorsOfNormals[i].equ_power}</p>
  <p class="longitudeAndLatitude">监测范围：${sensorsOfNormals[i].equ_scope}</p>
  <p class="longitudeAndLatitude">电压：${sensorsOfNormals[i].equ_voltage}</p>
  </div>`
  var infoWindow = new BMap.InfoWindow(sContent);  // 创建信息窗口对象
  map.openInfoWindow(infoWindow,point); //开启信息窗口
}

 // 报警时信息窗口
 function infoWindowOfAlert(alterId){
  var sContent =`<div class="father">
  <h4>灾情报告</h4>
  <p class="typeOfDisaster">灾情类型：${sensorsOfAlter[alterId].siren_rank}</p>
  <p class="disasterLevel">级别：${sensorsOfAlter[alterId].siren_type}</p>
  <p class="longitudeAndLatitude">传感器：${sensorsOfAlter[alterId].equ_cid}</p>
  <button id="pid" onclick="alertAndhHndling(this)">警报处理</button>
  </div>`
  var infoWindow = new BMap.InfoWindow(sContent);  // 创建信息窗口对象
  map.openInfoWindow(infoWindow,alertPoint[alterId]); //开启信息窗口
 }

 // 故障时信息窗口
 function infoWindowOfFault(faultId){
  var sContent =`<div class="father">
  <h4>故障报告</h4>
  <p class="longitudeAndLatitude">传感器故障：${sensorsOfFault[faultId].equ_cid}</p>
  <p class="typeOfDisaster">故障类型：${sensorsOfFault[faultId].fault_type}</p>
  </div>`
  var infoWindow = new BMap.InfoWindow(sContent);  // 创建信息窗口对象
  map.openInfoWindow(infoWindow,faultPoint[faultId]); //开启信息窗口
 }

// 警报处理
 function alertAndhHndlings(a){
  console.log(a)
}

